//
//  HotspotViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/29.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "HotspotViewController.h"
#import "UIView+Frame.h"
#import "JHMacro.h"
#import "TopScrollMenu.h"

#import "CartoonViewController.h"
#import "NovelViewController.h"
#import "RadioStationViewController.h"
@interface HotspotViewController ()<TopScrollMenuDelegate,UIScrollViewDelegate>
//@property (nonatomic,strong)NSArray *titles;
//@property (nonatomic,strong)NSMutableArray *pageViews;


@property (nonatomic,strong)TopScrollMenu *topScrollMenu;
@property (nonatomic,strong)UIScrollView *mainScrollView;
@property (nonatomic,strong)NSArray *titles;



@end

@implementation HotspotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSURL *url = [NSURL URLWithString:@"http://www.webtoons.com/zh-hans/"];
//    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
//    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20,self.view.frame.size.width , self.view.frame.size.height - 68)];
//    [self.view addSubview:webView];
//    webView.scrollView.bounces = NO;
//    [webView loadRequest:request];
    
//    [self createHeadCollectionView];
    
    [self setupChildViewController];

//    self.titles = @[@"漫画吧",@"小说吧",@"电台吧"];
    self.titles = @[@"漫画吧"];

    self.topScrollMenu = [TopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    
    _topScrollMenu.staticTitleArr = [NSArray arrayWithArray:_titles];
    
    _topScrollMenu.topScrollMenuDelegate = self;
    
    [self.view addSubview:_topScrollMenu];
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _titles.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self showVc:0];
    
    [self.view insertSubview:_mainScrollView belowSubview:_topScrollMenu];
}

#pragma mark - - - TopScrollMenu代理方法
- (void)TopScrollMenu:(TopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}

- (void)setupChildViewController {

    
    CartoonViewController *cartoonView = [[CartoonViewController alloc] init];
    [self addChildViewController:cartoonView];
    
//    RadioStationViewController *radioStation = [[RadioStationViewController alloc] init];
//    [self addChildViewController:radioStation];
//
//    
//    NovelViewController *novelVC = [[NovelViewController alloc] init];
//    [self addChildViewController:novelVC];
    
    }

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    UILabel *selLabel = self.topScrollMenu.allTitleLabel[index];
    
    [self.topScrollMenu staticTitleLabelSelecteded:selLabel];
    

    
}



//- (void)createHeadCollectionView{
//    _titles = @[@"发现", @"社会摇", @"八卦震", @"涨姿势", @"笑点低", @"段子手", @"二次元", @"电影范", @"动物园", @"星座控", @"爱搞机", @"军事迷", @"体育圈"];
//    HACursor *cursorCollectionView = [[HACursor alloc] init];
//    cursorCollectionView.frame = CGRectMake(0, 20, SCREEN_WIDTH, 44);
//    cursorCollectionView.titles = _titles;
//    cursorCollectionView.pageViews = [self createPageViews];
//    cursorCollectionView.rootScrollViewHeight = SCREEN_HEIGHT - 109;
//    //显示排序按钮
//    cursorCollectionView.showSortbutton = YES;
//    cursorCollectionView.titleNormalColor = [UIColor blackColor];
//    cursorCollectionView.titleSelectedColor = [UIColor redColor];
//    cursorCollectionView.backgroundColor = [UIColor colorWithRed:0.99 green:0.81 blue:0.00 alpha:1.00];
//    //设置缩小放大字体
//    cursorCollectionView.minFontSize = 15;
//    cursorCollectionView.maxFontSize = 20;
//    //设置是否需要渐变字体的大小
//    cursorCollectionView.isGraduallyChangFont = YES;
//    [self.view addSubview:cursorCollectionView];
//}
//- (NSMutableArray *)createPageViews{
//

//    _pageViews = [NSMutableArray array];
//    NSArray *webViewArray = @[@"http://www.youku.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com",@"http://www.baidu.com", @"http://www.youku.com"];
//    for (int i = 0; i < _titles.count; i ++) {
//        UIWebView *web = [[UIWebView alloc]init];
//        NSString *str = [NSString stringWithFormat:@"%@",[webViewArray objectAtIndex:i]];
//        NSURL *url = [NSURL URLWithString:str];
//        NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:url];
//        [web loadRequest:urlRequest];
//        [_pageViews addObject:web];
//    }
//    return _pageViews;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
